package com.glwan.fixedassetapp.components;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.common.CommonConstant;

public class ScrollPageView extends LinearLayout {

    private int totalCount = 0;

    private int currentCount = 0;

    private LinearLayout pullLoading;

    private LinearLayout noMoreData;

    private ListView listView;

    private OnScrollPageListener onScrollPageListener;

    public ScrollPageView(Context context) {
        this(context, null);
    }

    public ScrollPageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ScrollPageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public ScrollPageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initData();
    }

    private void initData() {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View inflate = layoutInflater.inflate(R.layout.loading_more, null);
        inflate.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));

        pullLoading = inflate.findViewById(R.id.pullLoading);

        noMoreData = inflate.findViewById(R.id.noMoreData);

        addView(inflate);


    }

    /**
     * 设置监听器
     *
     * @param onScrollPageListener
     */
    public void setOnScrollPageListener(OnScrollPageListener onScrollPageListener) {
        this.onScrollPageListener = onScrollPageListener;
    }

    /**
     * 设置滚动监听
     *
     * @param open
     */
    public void setScrollListener(boolean open) {
        if (open) {
            listView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                        Log.i("当前列表总行数", String.valueOf(view.getLastVisiblePosition()));
                        Log.i("list大小", String.valueOf(listView.getCount()));
                        Log.i("totalCount", String.valueOf(totalCount));
                        Log.i("currentCount", String.valueOf(currentCount));
                        if (view.getLastVisiblePosition() == listView.getCount() - 1) {
                            if (view.getLastVisiblePosition() >= totalCount - 1) {
                                pullLoading.setVisibility(View.GONE);
                                noMoreData.setVisibility(View.VISIBLE);
                                Log.i("currentCount！！！", String.valueOf(currentCount));

                            } else {
                                pullLoading.setVisibility(View.VISIBLE);
                                noMoreData.setVisibility(View.GONE);
                                setScrollListener(false);
                                currentCount = currentCount + CommonConstant.INIT_LIST_VIEW_SIZE;
                                Log.i("canScrollCurrentCount", String.valueOf(currentCount));
                                if (onScrollPageListener != null) {
                                    onScrollPageListener.onScrollEndPage();
                                }
                            }
                        }

                    }
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                }
            });
        } else {
            listView.setOnScrollListener(null);
        }
    }

    /**
     * 设置listview
     *
     * @param listView listView
     */
    public void setListView(ListView listView) {
        this.listView = listView;
    }

    public int getCurrentCount() {
        return currentCount;
    }

    public void setCurrentCount(int currentCount) {
        this.currentCount = currentCount;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * ScrollPage 监听器内部类
     */
    public interface OnScrollPageListener {
        void onScrollEndPage();
    }
}
